package com.jubaozan.service.platform.decorator;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jubaozan.c3.framework.utils.ObjectUtil;
import com.jubaozan.c3.framework.utils.ResultHolder;
import com.jubaozan.dao.youme.domain.Store;
import com.jubaozan.dao.youme.domain.StoreAudit;
import com.jubaozan.dao.youme.domain.StoreUser;
import com.jubaozan.dao.youme.service.StoreAuditService;
import com.jubaozan.dao.youme.service.StoreService;
import com.jubaozan.dao.youme.service.StoreUserService;
import com.jubaozan.feign.level.LevelFeignService;
import com.jubaozan.feign.level.vo.LevelVO;
import com.jubaozan.service.platform.constants.PlatformConstants;
import com.jubaozan.service.platform.constants.PlatformStatusEnum;
import com.jubaozan.service.platform.constants.PlatformTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @ClassName PlatformAuditDecorator
 * @Description 个体平台入驻审核Decorator
 * @Author SunWei
 * @Version 1.0
 **/
@Slf4j
@Component
public class PlatformAuditDecorator {

    private final StoreAuditService storeAuditService;

    private final StoreService storeService;

    private final StoreUserService storeUserService;

    private final LevelFeignService levelFeignService;

    public PlatformAuditDecorator(StoreAuditService storeAuditService, StoreService storeService, StoreUserService storeUserService, LevelFeignService levelFeignService) {
        this.storeAuditService = storeAuditService;
        this.storeService = storeService;
        this.storeUserService = storeUserService;
        this.levelFeignService = levelFeignService;
    }

    /**
     * 审核通过
     * @param reviewStatus
     * @param storeAudit
     * @return
     */
    public Boolean pass( Boolean reviewStatus,StoreAudit storeAudit){
        logger.info("审核对象{}", JSONObject.toJSONString(storeAudit));
        Store store = storeService.getById(storeAudit.getStoreId());
        //个人平台审核
        if(PlatformTypeEnum.PERSONAL_PLATFORM.type().equals(store.getType())){
            //审核通过
            storeAudit.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
            //状态修改为审核通过
            store.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
            store.setFirstAudit(LocalDateTime.now());
        }else{
            //企业平台审核
            if(PlatformStatusEnum.WAIT_REPEAT_AUDIT.status().equals(store.getStatus())){
                //审核通过
                storeAudit.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
                //状态修改为审核通过
                store.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
                store.setFirstAudit(LocalDateTime.now());
            }else{
                //是否需要复审
                if(reviewStatus){
                    //审核记录修改为复审中
                    storeAudit.setStatus(PlatformStatusEnum.PUT_RECHECK_DATA.status());
                    //状态修改为提交复审资料
                    store.setStatus(PlatformStatusEnum.PUT_RECHECK_DATA.status());
                }else {
                    //审核记录修改审核通过
                    storeAudit.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
                    //状态修改为审核通过
                    store.setStatus(PlatformStatusEnum.PASS_AUDIT.status());
                }
            }
        }

        //保存审核记录
        Boolean result = storeAuditService.saveOrUpdate(storeAudit,new QueryWrapper<StoreAudit>().eq("store_id", storeAudit.getStoreId()));
        if(result){
            store.setId(storeAudit.getStoreId());
            store.setUpdateAt(LocalDateTime.now());
            //修改门店信息状态
            storeService.updateById(store);
            //保存门店信息
            StoreUser storeUser = new StoreUser();
            StoreUser serviceOne = storeUserService.getOne(new QueryWrapper<StoreUser>().lambda().eq(StoreUser::getStoreId, storeAudit.getStoreId()).eq(StoreUser::getUserId, store.getUserId()));
            if(ObjectUtil.isEmpty(serviceOne)){
                ResultHolder<LevelVO> level = levelFeignService.queryLevel(PlatformConstants.STORE_LEVEL, 0L);
                storeUser.setLevelId(level.getBody().getId());
            }
            storeUser.setStoreId(storeAudit.getStoreId());
            storeUser.setUserId(store.getUserId());
            storeUser.setRefereeId(store.getRefereeId());
            storeUser.setCreateAt(LocalDateTime.now());
            result = storeUserService.saveOrUpdate(storeUser, new QueryWrapper<StoreUser>().eq("store_id", storeAudit.getStoreId()).eq("user_id", store.getUserId()));
        }
        return result;
    }

    /**
     * 审核拒绝
     * @param storeAudit
     * @return
     */
    public Boolean refuse(StoreAudit storeAudit){
        Store store = storeService.getById(storeAudit.getStoreId());
        //个人平台审核
        if(PlatformTypeEnum.PERSONAL_PLATFORM.type().equals(store.getType())){
            //审核不通过
            storeAudit.setStatus(PlatformStatusEnum.REFUSE_AUDIT.status());
            //状态修改为审核不通过
            store.setStatus(PlatformStatusEnum.REFUSE_AUDIT.status());
        }else{
            //企业平台审核
            if(PlatformStatusEnum.WAIT_REPEAT_AUDIT.status().equals(store.getStatus()) ){
                //复审不通过
                storeAudit.setStatus(PlatformStatusEnum.REFUSE_REPEAT_AUDIT.status());
                //状态修改为复审不通过
                store.setStatus(PlatformStatusEnum.REFUSE_REPEAT_AUDIT.status());
            }else{
                //初审不通过
                storeAudit.setStatus(PlatformStatusEnum.REFUSE_AUDIT.status());
                //状态修改为初审不通过
                store.setStatus(PlatformStatusEnum.REFUSE_AUDIT.status());
            }
        }

        //保存审核记录
        Boolean result = storeAuditService.saveOrUpdate(storeAudit,new QueryWrapper<StoreAudit>().eq("store_id", storeAudit.getStoreId()));
        if(result){
            store.setId(storeAudit.getStoreId());
            store.setUpdateAt(LocalDateTime.now());
            result = storeService.updateById(store);
        }
        return result;
    }

}
